package com.zws.cucumber.expr.unit;

import com.zws.cucumber.expr.ZwsExprFunc;
import com.zws.cucumber.expr.ZwsExprMode;
import com.zws.cucumber.expr.ZwsExprUnit;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Set;

/**
 * author: zws
 */
public class ZwsExprUnitActualTransformationImpl implements ZwsExprUnit {

    private ZwsExprUnit origin;

    public ZwsExprUnitActualTransformationImpl(ZwsExprUnit origin) {
        this.origin = origin;
    }

    @Override
    public String name() {
        return origin.name();
    }

    @Override
    public Set<String> aliases() {
        return origin.aliases();
    }

    @Override
    public ZwsExprMode mode() {
        return origin.mode();
    }

    @Override
    public ZwsExprFunc func() {
        return generateActualTransformationFunction(origin.func());
    }

    // swap the actual and the pattern
    private static ZwsExprFunc generateActualTransformationFunction(ZwsExprFunc func) {
        return (ctx, args, pattern, actual) -> {
            Pair<Object, Object> result = func.apply(ctx, args, (String) actual, pattern);
            return Pair.of(result.getRight(), result.getLeft());
        };
    }

    @Override
    public String toString() {
        return "transforming actual by " + name();
    }
}
